package com.zukxu.items.project.pinyin4j;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

/**
 * Description:
 *
 * @author zukxu
 * @date 2020/10/30 0030 09:26
 */
public class PinYinTool {

	/**
	 * 字符串转拼音
	 * @param code
	 * @return
	 */
	public Map<String, StringBuffer> string2Pinyin(String code) {
		//全拼
		StringBuffer pinyinAllStr = new StringBuffer();
		//首字母
		StringBuffer pinyinFirstStr = new StringBuffer();
		//将字符串转换为字符数组
		char[] newChar = code.toCharArray();
		//定义转换拼音格式
		HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
		//设置拼音转换大小写格式
		defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
		//设置声调转换方式
		defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		//设置特殊拼音ü的显示格式：例如lv,设置以v进行显示
		defaultFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
		//设置分隔符
		String regex = ",";
		for (char c : newChar) {
			if (c > 128) {
				try {
					//获得单个汉字全部拼音（多音字）
					String[] str = PinyinHelper.toHanyuPinyinStringArray(c, defaultFormat);
					if (str != null) {
						if (pinyinAllStr.length() <= 0) {
							//初始化
							for (int j = 0; j < str.length; j++) {
								pinyinAllStr.append(str[j]);
								pinyinFirstStr.append(str[j].charAt(0));
								if (j != str.length - 1) {
									pinyinAllStr.append(regex);
									pinyinFirstStr.append(regex);
								}
							}
							continue;
						}
						pinyinAllStr = getAllPinyin(str, pinyinAllStr, regex);
						pinyinFirstStr = getFirstPinyin(str, pinyinFirstStr, regex);
					}
				} catch (BadHanyuPinyinOutputFormatCombination e) {
					e.printStackTrace();
				}
			} else {
				pinyinAllStr.append(c);
				pinyinFirstStr.append(c);
			}
		}
		HashMap<String, StringBuffer> map = new HashMap<>();
		map.put("all", pinyinAllStr);
		map.put("first", pinyinFirstStr);
		return map;
	}

	/**
	 * 获取汉字的首字母
	 *
	 * @param str            汉字拼音
	 * @param pinyinFirstStr 已经存在的首字母
	 * @param regex          分隔符
	 * @return 首字母
	 */
	private StringBuffer getFirstPinyin(String[] str, StringBuffer pinyinFirstStr, String regex) {
		String[] firstPinyin = new String(pinyinFirstStr).toUpperCase().split(regex);
		StringBuffer temp = new StringBuffer();
		HashSet<String> set = new HashSet<>(Arrays.asList(firstPinyin));
		Object[] objects = set.toArray();
		for (int i = 0; i < objects.length; i++) {
			for (int j = 0; j < str.length; j++) {
				temp.append(objects[i]).append(str[j].charAt(0));
				if (j != str.length - 1) {
					temp.append(regex);
				}
			}
			if (i != objects.length - 1) {
				temp.append(regex);
			}
		}
		pinyinFirstStr = temp;

		return pinyinFirstStr;
	}

	/**
	 * 获取汉字组合的全拼
	 *
	 * @param str       汉字拼音
	 * @param pinyinAllStr 已经存在的全拼
	 * @param regex     分隔符
	 * @return 全拼
	 */
	private StringBuffer getAllPinyin(String[] str, StringBuffer pinyinAllStr, String regex) {
		//单音字直接转换存储
		StringBuffer temp = new StringBuffer();
		String[] split = new String(pinyinAllStr).split(regex);
		//单音字
		if (str.length == 1) {
			for (int j = 0; j < split.length; j++) {
				temp.append(split[j]).append(str[0]);
				if (j != split.length - 1) {
					temp.append(regex);
				}
			}
		} else {
			//多音字
			for (int j = 0; j < split.length; j++) {
				for (int k = 0; k < str.length; k++) {
					temp.append(split[j]).append(str[k]);
					if (k != str.length - 1) {
						temp.append(regex);
					}
				}
				if (j != split.length - 1) {
					temp.append(regex);
				}
			}
		}
		pinyinAllStr = temp;
		return pinyinAllStr;
	}

}
